package com.sec.etech.file.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import org.openbpm.base.api.exception.BusinessException;
import org.openbpm.base.core.util.PropertyUtil;
import org.openbpm.sys.upload.OrdinaryUploader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;

@Service
public class EtechOrdinaryUploader extends OrdinaryUploader {
    @Override
    public String type() {
        return "etech-ordinary";
    }


    public String upload(InputStream is, String name) {
        String path = this.getPath(name);
        String absPath = this.getAbsolutePath(path);
        if(!FileUtil.exist(new File(absPath).getParentFile())){
            FileUtil.mkParentDirs(absPath);
        }
        FileUtil.writeFromStream(is, absPath);
        return path;
    }

    public File takeFile(String path) {
        try {
            String realPath = getAbsolutePath(path);
            return new File(realPath);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    @Override
    public InputStream take(String path) {
        try {
            String realPath = getAbsolutePath(path);
            return new FileInputStream(new File(realPath));
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    @Override
    public void remove(String path) {
        String realPath = getAbsolutePath(path);
        FileUtil.del(realPath);
    }

    public String getAbsolutePath(String path){
        String absolutePath = path;
        // 相对目录
        if(!absolutePath.startsWith("/")){
            absolutePath = getUploadBasePath() + path;
        }else {
            //兼容旧数据
            if(path!=null && path.startsWith("/app/data/files")){
                absolutePath = getUploadBasePath() + path.substring("/app/data/files".length());
            }
        }
        return absolutePath;
    }

    protected String getUploadBasePath(){
        // 优先获取环境变量 uploader.ordinary.path
//        String uploadBasePath = PropertyUtil.getProperty("uploader.ordinary.path", uploaderOrdinaryPathDefault);
        //todo 这里改为取默认的旧上传器的地址，后面再优化
        String uploadBasePath = uploaderOrdinaryPathDefault;
        if(!uploadBasePath.endsWith("/")){
            uploadBasePath = uploadBasePath + "/";
        }
        return uploadBasePath;
    }

    protected String getPath(String name) {
        Date today = new Date();
        String path =  DateUtil.format(today, "yyyy") + "/"
                +  DateUtil.format(today, "MM") + "/"
                +  DateUtil.format(today, "dd") + "/";
        path = path + name;
        return path;
    }

    @Value("${etech.file.upload-dir:/app/data2/files/uploads/}")
    private String uploaderOrdinaryPathDefault;

    public static String getFilePath(String fileName){
        Date today = new Date();
        String path =  DateUtil.format(today, "yyyy") + "/"
                +  DateUtil.format(today, "MM") + "/"
                +  DateUtil.format(today, "dd") + "/";
        path = path + fileName;
        return path;
    }


}
